﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InWms
{
    public partial class InStockWnd : UserControl
    {
        #region 成员变量
        BilInBill CurrentInBill { get; set; }
        #endregion

        #region 构造函数
        public InStockWnd()
        {
            InitializeComponent();
            InitBtnImage();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnSelectInBill.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnAssignLocation.Image = global::Zwms.Client.Properties.Resources.category;
            this.btnInBillReport.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnLocationList.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnCancelBill.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }
        private void InitBill(BilInBill record)
        {
            if (record == null)
            {
                return;
            }
            tbInBillCode.Text = record.Id;
            tbInPlanBillCode.Text = record.IBPlanCode;
            tbBillStatus.Text = CommonDefine.DictInBillStatus[record.BillStatus];

            ServiceReferenceZwms.ServiceZwmsClient client
               = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInBillDetail queryRecord = new BilInBillDetail();
            queryRecord.IBCode = record.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<BilInBillDetail> inBilDetailList =
                client.QueryInBillDetailList(queryRecord, ref pagerIndex);

            this.bilInBillDetailBindingSource.DataSource = null;
            if (inBilDetailList.Count > 0)
            {
                this.bilInBillDetailBindingSource.DataSource = inBilDetailList;
            }
        }

        private void ClearControl()
        {
            tbInBillCode.Text = "";
            tbInPlanBillCode.Text = "";
            tbBillStatus.Text = "";

            bilInBillDetailBindingSource.DataSource = null;
        }

        private void InBillSelect()
        {
            QueryInBillWnd uc = new QueryInBillWnd();
            ContainerForm window = new ContainerForm(uc, "选择入库计划单");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (uc.SelectedRecord.BillStatus == "正在收货")
                {
                    MessageBox.Show("当前订单尚未完成收货！");
                    btnAssignLocation.Enabled = false;
                    btnInBillReport.Enabled = false;
                    CurrentInBill = null;
                }
                else if (uc.SelectedRecord.BillStatus == "已入库")
                {
                    CurrentInBill = uc.SelectedRecord;
                    btnAssignLocation.Enabled = false;
                    btnInBillReport.Enabled = true;
                }
                else
                {
                    btnAssignLocation.Enabled = true;
                    btnInBillReport.Enabled = true;
                    CurrentInBill = uc.SelectedRecord;
                }

                if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
                {
                    //无限制
                }
                else
                {
                    //判断是否为该用户的入库计划单
                    if (uc.SelectedRecord.CreateUserCode.Trim() =="")
                    {
                        //无限制
                    }
                    else if (uc.SelectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                    {
                        MessageBox.Show("对不起，您不具备该入库单的操作权限！");
                        return;
                    }
                }
                
                InitBill(CurrentInBill);
            }
        }
        #endregion

        #region 控件函数

        private void btnSelectInBill_Click(object sender, EventArgs e)
        {
            InBillSelect();
        }

        private void btnAssignLocation_Click(object sender, EventArgs e)
        {
            if (CurrentInBill == null)
            {
                MessageBox.Show("请首先选择订单！");
                return;
            }
            if (CurrentInBill.BillStatus != "1")  //1 正在上架
            {
                MessageBox.Show("订单状态不符合要求！");
                return;
            }

            AssignLocationForm window = new AssignLocationForm(CurrentInBill);
            if (window.ShowDialog() == DialogResult.OK)
            {
                InitBill(CurrentInBill);
            }

        }

        private void btnInBillReport_Click(object sender, EventArgs e)
        {
            if (CurrentInBill == null)
            {
                MessageBox.Show("请首先选择订单！");
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
             = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInBillDetail queryRecord = new BilInBillDetail();
            queryRecord.IBCode = CurrentInBill.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<BilInBillDetail> inBilDetailList =
                client.QueryInBillDetailList(queryRecord, ref pagerIndex);
            Report.ReportHelper.PrintInBill(CurrentInBill, inBilDetailList.ToList());
        }

        private void btnLocationList_Click(object sender, EventArgs e)
        {
            if (CurrentInBill == null)
            {
                MessageBox.Show("请首先选择订单！");
                return;
            }

            if (CurrentInBill.BillStatus != "2")  //1 正在上架
            {
                MessageBox.Show("订单尚未分配库位！");
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
             = new ServiceReferenceZwms.ServiceZwmsClient();
            StoGoodsStock queryRecord = new StoGoodsStock();
            queryRecord.IBCode = CurrentInBill.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<StoGoodsStock> inGoodsStockList =
                client.QueryGoodsStockList(queryRecord, ref pagerIndex);
            Report.ReportHelper.PrintLocationArrange(inGoodsStockList.ToList());
        }

        private void btnCancelBill_Click(object sender, EventArgs e)
        {
            if (CurrentInBill == null)
            {
                MessageBox.Show("请首先选择入库单！");
                return;
            }

            if (CurrentInBill.BillStatus == "2")
            {
                MessageBox.Show("该入库单已完成，无法撤销！");
                return;
            }
            else if(CurrentInBill.BillStatus == "3")
            {
                MessageBox.Show("该入库单已撤销！");
                return;
            }

            if (MessageBox.Show(string.Format("是否确认撤销入库单 {0}？\r\n (注意：撤销后将无法恢复)", CurrentInBill.Id.Trim()), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            CurrentInBill.BillStatus = "3";
            if (client.UpdateInBill(CurrentInBill))
            {
                InitBill(CurrentInBill);
                MessageBox.Show("成功！");
            }
        }
        #endregion
    }
}
